package cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterial;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterial.vo.PickTaskMaterialPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterial.vo.PickTaskMaterialRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterial.vo.PickTaskMaterialSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.picktaskmaterial.PickTaskMaterialDO;
import cn.iocoder.yudao.module.hs.service.picktaskmaterial.PickTaskMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 拣货任务机型明细")
@RestController
@RequestMapping("/hs/pick-task-material")
@Validated
public class PickTaskMaterialController {

    @Resource
    private PickTaskMaterialService pickTaskMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建拣货任务机型明细")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material:create')")
    public CommonResult<Long> createPickTaskMaterial(@Valid @RequestBody PickTaskMaterialSaveReqVO createReqVO) {
        return success(pickTaskMaterialService.createPickTaskMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拣货任务机型明细")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material:update')")
    public CommonResult<Boolean> updatePickTaskMaterial(@Valid @RequestBody PickTaskMaterialSaveReqVO updateReqVO) {
        pickTaskMaterialService.updatePickTaskMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拣货任务机型明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material:delete')")
    public CommonResult<Boolean> deletePickTaskMaterial(@RequestParam("id") Long id) {
        pickTaskMaterialService.deletePickTaskMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拣货任务机型明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material:query')")
    public CommonResult<PickTaskMaterialRespVO> getPickTaskMaterial(@RequestParam("id") Long id) {
        PickTaskMaterialDO pickTaskMaterial = pickTaskMaterialService.getPickTaskMaterial(id);
        return success(BeanUtils.toBean(pickTaskMaterial, PickTaskMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拣货任务机型明细分页")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material:query')")
    public CommonResult<PageResult<PickTaskMaterialRespVO>> getPickTaskMaterialPage(@Valid PickTaskMaterialPageReqVO pageReqVO) {
        PageResult<PickTaskMaterialDO> pageResult = pickTaskMaterialService.getPickTaskMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PickTaskMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拣货任务机型明细 Excel")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPickTaskMaterialExcel(@Valid PickTaskMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PickTaskMaterialDO> list = pickTaskMaterialService.getPickTaskMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拣货任务机型明细.xls", "数据", PickTaskMaterialRespVO.class,
                        BeanUtils.toBean(list, PickTaskMaterialRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得拣货任务机型明细list")
    @Parameter(name = "id", description = "拣货任务ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material:query')")
    public CommonResult<List<PickTaskMaterialRespVO>> getPickTaskMaterialList(@RequestParam("pickTaskId") Long pickTaskId) {
        List<PickTaskMaterialDO> pickTaskMaterialList = pickTaskMaterialService.getPickTaskMaterialList(pickTaskId);
        return success(BeanUtils.toBean(pickTaskMaterialList, PickTaskMaterialRespVO.class));
    }
}